package com.player;

import java.util.Hashtable;
import java.util.Vector;

import android.app.Activity;
import android.content.res.Resources;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.cardgame.R;
import com.cardgame.constants.Constants;

@SuppressWarnings("unchecked")
public class Player {
	
	private int currentHP;
	private String gender;
	private Boolean selected = false;
	//store the cards currently in hand
	private Vector inHands;
	
	private String skill1;
	private String skill2;
	
	private String name;
	private int team;
	private String pClass;
	
	//the  spells that are casted on this player
	private Vector playerState;
	
	//the rounds of poison
	private int poisonRound = 0;
	
	//the duration for negation
	private int negationCount = 0;
	
	//the duration for shallow grave 
	private int shallowCount = 0;
	
	ImageView action;
	ImageButton portrait;
	ImageView cardNumber;
	ImageView hpImage;
	ImageView statusa;
	ImageView statusb;
	ImageView statusc;
	
	Resources mRes;
	Activity myActivity;
	Hashtable statusImages;

	
	public Player()
	{
		playerState = new Vector();
		this.selected = false;
		initHashtable();
	}
	
	private void initHashtable()
	{
		statusImages = new Hashtable();
		statusImages.put(Constants.SpiritName, R.drawable.ethereal_status);
		statusImages.put(Constants.HexName, R.drawable.hex_status);
		statusImages.put(Constants.NegationName, R.drawable.negation_status);
		statusImages.put(Constants.PoisonName, R.drawable.poison_status);
		statusImages.put(Constants.ShallowName, R.drawable.shallowgrave_status);
		statusImages.put(Constants.SilenceName, R.drawable.silence_status);
	}
	
	public int getCurrentHP() {
		return currentHP;
	}

	public void setCurrentHP(int currentHP) {
		this.currentHP = currentHP;
	}

	public Boolean getSelected() {
		return selected;
	}

	public void setSelected(Boolean selected) {
		this.selected = selected;
	}

	public Vector getInHands() {
		return inHands;
	}

	public void setInHands(Vector inHands) {
		this.inHands = inHands;
	}

	public String getSkill1() {
		return skill1;
	}

	public void setSkill1(String skill1) {
		this.skill1 = skill1;
	}

	public String getSkill2() {
		return skill2;
	}

	public void setSkill2(String skill2) {
		this.skill2 = skill2;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getTeam() {
		return team;
	}

	public void setTeam(int team) {
		this.team = team;
	}

	public String getpClass() {
		return pClass;
	}

	
	public void clearAllImages()
	{
		playerState = new Vector();
		
		myActivity.runOnUiThread(new Runnable()
		{
			public void run(){
				portrait.setBackgroundDrawable(null);
				portrait.setClickable(false);
				hpImage.setBackgroundDrawable(null);
				cardNumber.setBackgroundDrawable(null);
				action.setBackgroundDrawable(null);
				statusa.setBackgroundDrawable(null);
				statusb.setBackgroundDrawable(null);
				statusc.setBackgroundDrawable(null);
			}
		});
	}
	
	public void clearPortraitImage()
	{
		myActivity.runOnUiThread(new Runnable()
		{
			public void run(){
				portrait.setBackgroundDrawable(null);
			}
		});
	}
	
	public void setStatusImages()
	{
		myActivity.runOnUiThread(new Runnable()
		{
			public void run(){
				if(playerState.size() > 0)
				{
					if(statusImages.get(playerState.elementAt(0)) != null)
					{
						statusa.setBackgroundDrawable(mRes.getDrawable((Integer)statusImages.get(playerState.get(0))));
					}
				}
				else{
					statusa.setBackgroundDrawable(null);
				}
				
				if(playerState.size() > 1)
				{
					if(statusImages.get(playerState.elementAt(1)) != null)
					{
						statusb.setBackgroundDrawable(mRes.getDrawable((Integer)statusImages.get(playerState.get(1))));
					}
				}
				else{
					statusb.setBackgroundDrawable(null);
				}
				
				if(playerState.size() > 2)
				{
					if(statusImages.get(playerState.elementAt(2)) != null)
					{
						statusc.setBackgroundDrawable(mRes.getDrawable((Integer)statusImages.get(playerState.get(2))));
					}
				}
				else{
					statusc.setBackgroundDrawable(null);
				}
			}
		});
	}
	
	//calculate the damaged based on the spell cards currently effective on this player
	public int takeDamage(int num)
	{
		int damage = num;
		
		Vector state = getPlayerState();
		for(int i = 0; i < state.size(); i++)
		{
			if((String)state.get(i) == Constants.FateName)
			{
				int diceRoll = rollDice();
				if(diceRoll == 1)
				{
					damage -= 1;
				}
				else if(diceRoll == 5 || diceRoll == 6)
				{
					damage += 1;
				}
				state.remove(i);
				i--;	
			}
			
			else if((String)state.get(i) == Constants.HexName)
			{
				damage += 1;
				state.remove(i);
				i--;	
			}
		}
		setCurrentHP(getCurrentHP() - damage);
		
		setStatusImages();
		
		return damage;
	}
	
	//remove the continuous spells that met the end condition
	public void removeState()
	{

		Vector state = getPlayerState();
		for(int i = 0; i < state.size(); i++)
		{
			if((String)state.get(i) == Constants.FateName)
			{
				state.remove(i);
				i--;	
			}
			
			else if((String)state.get(i) == Constants.HexName)
			{
				state.remove(i);
				i--;	
			}
		}
		
		setStatusImages();
	}
	
	public void poisonDamage()
	{
		int diceRoll = rollDice();
		if(diceRoll % 2 != 0)
		{
			setCurrentHP(getCurrentHP() - 1);
		}
		setPoisonRound(getPoisonRound() - 1);
	}
	
	public void setpClass(String pClass) {
		this.pClass = pClass;
		
		if(pClass == Constants.classSage)
		{
			setSkill1(Constants.skill1);
			setSkill2(Constants.skill2);
			setGender(Constants.male);
		}
		else if(pClass == Constants.classProphet)
		{
			setSkill1(Constants.skill3);
			setSkill2(Constants.skill4);
			setGender(Constants.female);
		}
		
		else if(pClass == Constants.classSorceress)
		{
			setSkill1(Constants.skill5);
			setSkill2(Constants.skill6);
			setGender(Constants.female);
		}
		
		else if(pClass == Constants.classVampire)
		{
			setSkill1(Constants.skill7);
			setSkill2(Constants.skill8);
			setGender(Constants.male);
		}
		
		else if(pClass == Constants.classAlchemist)
		{
			setSkill1(Constants.skill9);
			setSkill2(Constants.skill10);
			setGender(Constants.male);
		}
		
		else if(pClass == Constants.classFighter)
		{
			setSkill1(Constants.skill11);
			setSkill2(Constants.skill12);
			setGender(Constants.male);
		}
		
		else if(pClass == Constants.classIllusionist)
		{
			setSkill1(Constants.skill13);
			setSkill2(Constants.skill14);
			setGender(Constants.female);
		}
		
		/* else if(pClass == Constants.classRogue)
		{
			setSkill1(Constants.skill15);
			setSkill2(Constants.skill16);
		}*/
		
		else if(pClass == Constants.classPriestess)
		{
			setSkill1(Constants.skill17);
			setSkill2(Constants.skill18);
			setGender(Constants.female);
		}
		
		else if(pClass == Constants.classRanger)
		{
			setSkill1(Constants.skill19);
			setSkill2(Constants.skill20);
			setGender(Constants.female);
		}
		
		else if(pClass == Constants.classPaladin)
		{
			setSkill1(Constants.skill21);
			setSkill2(Constants.skill22);
			setGender(Constants.male);
		}
		
		else if(pClass == Constants.classAssassin)
		{
			setSkill1(Constants.skill23);
			setSkill2(Constants.skill24);
			setGender(Constants.male);
		}
		
		else if(pClass == Constants.classBard)
		{
			setSkill1(Constants.skill25);
			setSkill2(Constants.skill26);
			setGender(Constants.male);
		}
		
		else if(pClass == Constants.classAvenger)
		{
			setSkill1(Constants.skill27);
			setSkill2(Constants.skill28);
			setGender(Constants.female);
		}
	}

	public Vector getPlayerState() {
		return playerState;
	}

	public void setPlayerState(Vector playerState) {
		this.playerState = playerState;
		setStatusImages();
	}
	
	private int rollDice()
    {
    	int result = (int)(Constants.dice * Math.random()) + 1;
    	
    	return result;
    }

	public int getPoisonRound() {
		return poisonRound;
	}

	public void setPoisonRound(int poisonRound) {
		this.poisonRound = poisonRound;
	}
	
	
	//clear the spells that lasts one round
	public void clearStateAfter()
	{
		Vector state = this.getPlayerState();
		if(state.contains(Constants.SilenceName))
		{
			state.remove(Constants.SilenceName);
		}
		if(state.contains(Constants.SpiritName))
		{
			state.remove(Constants.SpiritName);
		}
		setPlayerState(state);
		
		if(negationCount > 0)
		{
			negationCount--;
		}
		else if(playerState.contains(Constants.NegationName))
		{
			playerState.remove(Constants.NegationName);
		}
		
		if(shallowCount > 0)
		{
			shallowCount--;
		}
		else if(playerState.contains(Constants.ShallowName))
		{
			playerState.remove(Constants.ShallowName);
		}
		
		setStatusImages();
		
	}

	public void clearStateBefore()
	{
		if(playerState.contains(Constants.NegationName) && negationCount == 0)
		{
			playerState.remove(Constants.NegationName);
		}
		else if(playerState.contains(Constants.NegationName) && negationCount > 0)
		{
			negationCount--;
		}
		
		if(playerState.contains(Constants.ShallowName) && shallowCount == 0)
		{
			playerState.remove(Constants.ShallowName);
		}
		else if(playerState.contains(Constants.ShallowName) && shallowCount > 0)
		{
			shallowCount--;
		}
		
		setStatusImages();
	}
	
	public int getNegationCount() {
		return negationCount;
	}

	public void setNegationCount(int negationCount) {
		this.negationCount = negationCount;
	}

	public int getShallowCount() {
		return shallowCount;
	}

	public void setShallowCount(int shallowCount) {
		this.shallowCount = shallowCount;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public ImageView getAction() {
		return action;
	}

	public void setAction(ImageView action) {
		this.action = action;
	}

	public ImageButton getPortrait() {
		return portrait;
	}

	public void setPortrait(ImageButton protrait) {
		this.portrait = protrait;
	}

	public ImageView getCardNumber() {
		return cardNumber;
	}

	public void setCardNumber(ImageView cardNumber) {
		this.cardNumber = cardNumber;
	}

	public ImageView getHpImage() {
		return hpImage;
	}

	public void setHpImage(ImageView hpImage) {
		this.hpImage = hpImage;
	}

	public ImageView getStatusa() {
		return statusa;
	}

	public void setStatusa(ImageView statusa) {
		this.statusa = statusa;
	}

	public ImageView getStatusb() {
		return statusb;
	}

	public void setStatusb(ImageView statusb) {
		this.statusb = statusb;
	}

	public ImageView getStatusc() {
		return statusc;
	}

	public void setStatusc(ImageView statusc) {
		this.statusc = statusc;
	}

	public Resources getmRes() {
		return mRes;
	}

	public void setmRes(Resources mRes) {
		this.mRes = mRes;
	}

	public Activity getMyActivity() {
		return myActivity;
	}

	public void setMyActivity(Activity myActivity) {
		this.myActivity = myActivity;
	}
	
}
